package main

import (
	"bufio"
	"bytes"
	"fmt"
	"log"
	"os"
	"os/exec"
	"strings"
)

func main() {

	for {
		// 读入一行，`\n`结束;
		inputReader := bufio.NewReader(os.Stdin)
		input, err := inputReader.ReadString('\n')
		if err != nil {
			log.Printf("[args error]:%s", err.Error())
			continue
		}

		seprateArgs := strings.Split(input, "|")

		var subTaskNum int = len(seprateArgs)
		var outBuf []*bufio.Reader = make([]*bufio.Reader, subTaskNum-1)

		for i := 0; i < subTaskNum; i++ {
			args := strings.Fields(seprateArgs[i])
			// fmt.Println("args:", args)
			cmd := exec.Command(args[0], args[1:]...)
			if i == 0 {
				cmd.Stdin = os.Stdin
				stdout, err := cmd.StdoutPipe()
				if err != nil {
					log.Println(err.Error())
				}
				outBuf[i] = bufio.NewReader(stdout)
			} else if i == subTaskNum-1 {

				stdin, err := cmd.StdinPipe()
				if err != nil {
					log.Println(err.Error())
				}
				outBuf[i-1].WriteTo(stdin)
				var outputBuf bytes.Buffer
				cmd.Stdout = &outputBuf
				cmd.Wait()
				fmt.Println(cmd.Stdout)
			} else {
				stdin, err := cmd.StdinPipe()
				if err != nil {
					log.Println(err.Error())
				}

			}
			cmd.Start()
		}

	}
}
